<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    
    <meta http-equiv="content-language" content="zh-CN" />
    

    
    <meta name="viewport" content="width=device-width, initial-scale=0.5">
    

    
    <title>画函数图像--R语言</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.8/clipboard.min.js"></script>
    
    
    
    
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css">

    
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap-theme.min.css">

    <link rel="stylesheet" href="/css/stylesheet.css">
    <link rel="stylesheet" href="/css/home.css">

    
    
        <style type="text/css">
        body { background-color: #fbf6ec;}
        </style>
    
    
                
        
        
            <link rel="stylesheet" href="/css/main.css"/>
        




        
        
        
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/styles/github.min.css"  />
         
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/highlight.min.js"></script>
        
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/r.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/yaml.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/latex.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/matlab.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/mathematica.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/julia.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/julia-repl.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/powershell.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/bash.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/shell.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/python.min.js"></script>
        
        <script>hljs.initHighlightingOnLoad();</script>
     <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
          
     <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css" integrity="sha512-+4zCK9k+qNFUR5X+cKL9EIR+ZOhtIloNl9GIKS57V1MyNsYpYcUrUeQc9vNfzsWfV28IaLL3i96P9sdNyeRssA==" crossorigin="anonymous" />
     
     
</head>


<body>
    <script>
        window.addEventListener("resize", resizeThrottler, false);

        var resizeTimeout;
        function resizeThrottler() {
        
        if ( !resizeTimeout ) {
            resizeTimeout = setTimeout(function() {
            resizeTimeout = null;
            actualResizeHandler();
        
            
            }, 66);
        }
        }
        actualResizeHandler()
        function actualResizeHandler() {
                if (/mobile/i.test(navigator.userAgent) || /android/i.test(navigator.userAgent))
                {
                    document.body.classList.add('mobile');
                }else{
                    document.body.classList.remove('mobile');  
                }
    }</script>

    
      
      
            <nav class="navbar navbar-default navbar-static-top" style="opacity: .9" role="navigation">
        <div class="container-fluid">
            
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">

                    <span class="sr-only">Toggle navigation</span>
                    <span class="big-icon icon-bar"></span>
                    <span class="big-icon icon-bar"></span>
                    <span class="big-icon icon-bar"></span>

                </button>
                <a class="navbar-brand" href="/">zsc</a>
            </div>

            <div class="navbar-collapse collapse" id="bs-example-navbar-collapse-1" style="height: auto;">
                <ul class="nav navbar-nav navbar-right" style="font-size: 100%">
                    
                        
                            
                            <li class=""><a href="/about/">About</a></li>
                            
                            <li class=""><a href="/categories/">Categories</a></li>
                            
                            <li class=""><a href="/">Home</a></li>
                            
                            <li class=""><a href="/tags/">Tags</a></li>
                            
                            <li class=""><a href="/issue/">存在的问题</a></li>
                            
                        
                    
                </ul>
            </div>
        </div>
    </nav>







<div class = "div-content" id='div-content-my' style='display: none;' >
    

    <div class = 'inner-left' id= 'divTableOfContents' style="position:fixed;z-index:999;height: 55%;overflow: scroll;bottom: 5%;width: 22%;top: 25%" >
            <p class="slide slidemy" align = "center">
                <a href="javascript:hidediv();" id="strHref" class="btn-slide">目录收起-</a>
            </p>
            
            <div id="divtocTableOfContents">
            <nav id="TableOfContents">
  <ul>
    <li><a href="#1-y--fx-的函数图像">1. $y = f(x)$ 的函数图像</a>
      <ul>
        <li><a href="#方法一-curve画函数图像">方法一: <code>curve()</code>画函数图像</a></li>
        <li><a href="#方法二-ggplot2">方法二: ggplot2</a></li>
      </ul>
    </li>
    <li><a href="#2-函数图像具有参数方程">2. 函数图像具有参数方程</a>
      <ul>
        <li><a href="#21-画x2y2--4的函数图像">2.1 画$x^2+y^2 = 4$的函数图像</a></li>
        <li><a href="#22-椭圆">2.2 椭圆</a></li>
        <li><a href="#23-抛物线">2.3 抛物线</a></li>
        <li><a href="#24-双曲线">2.4 双曲线</a></li>
        <li><a href="#24-lissajous-曲线利萨如曲线">2.4 Lissajous 曲线(利萨如曲线)</a></li>
      </ul>
    </li>
    <li><a href="#3-隐函数">3 .隐函数</a></li>
  </ul>
</nav>
            </div>
    </div>
</div>
<script>  
    $(document).ready(function () {
    var demo = $("#divtocTableOfContents").find("a").length;
    if(demo > 2){
        $("div#div-content-my").fadeIn("slow");
    }
        
        
        
        
        
        
        
        
    }); 
</script>  









<div class="inner">
    



    <div class="blog-post">
        
                <div>
            <h2 align="center" id = "singe-h2">
                画函数图像--R语言
                <time>
                    <br>
                    <span> 
                        <i class="fa fa-user-edit" style="color:#888;font-size: 80%;"></i>
                        zsc 
                    </span>
                    &nbsp 
                    <span>                 
                        <i class="fa fa-calendar-alt" style="color:#888;font-size: 80%;"></i>
                        2020-08-15 
                    </span>
                </time>
                
                
                <div>
                    <ul class="tags">
                        
                        <span>标签:</span>
                        <li><a class="link" href="/tags/%e5%87%bd%e6%95%b0%e5%9b%be%e5%83%8f"> #函数图像 </a></li><li><a class="link" href="/tags/r"> #r </a></li>
                        
                        <span> </span>
                        
                    </ul>
                    
                </div>
            </h2>
        </div>
    
        
        <section id="content">
            <p>据我了解, 计算机软件中画的函数图像大概有两种方法:</p>
<ul>
<li>方法一: 该图像有一系列密集点组成, 已达到欺骗人们的眼睛.感觉认为是连续的.</li>
<li>方法二: 为了解决方法一种的问题,把每两个点之间用直线连接已达到连续的状态</li>
</ul>
<p>在本文中我们将利用R语言来画函数图像&mdash;- 重点以<code>ggplot2</code>来展示</p>
<h2 id="1-y--fx-的函数图像">1. $y = f(x)$ 的函数图像</h2>
<p>比如:</p>
<p>$$
\begin{aligned}
y &amp;= sin(x),\
y &amp;= cos(x), \
y &amp;= \dfrac{1}{1+e^{(-x)}},\
y &amp;= x^2 .
\end{aligned}
$$</p>
<p>这是我们中学最常见的函数.</p>
<h3 id="方法一-curve画函数图像">方法一: <code>curve()</code>画函数图像</h3>
<p>所用函数调用格式</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-r" data-lang="r"><span style="display:flex;"><span><span style="color:#a6e22e">curve</span>(expr, from <span style="color:#f92672">=</span> <span style="color:#66d9ef">NULL</span>, to <span style="color:#f92672">=</span> <span style="color:#66d9ef">NULL</span>, n <span style="color:#f92672">=</span> <span style="color:#ae81ff">101</span>, add <span style="color:#f92672">=</span> <span style="color:#66d9ef">FALSE</span>,
</span></span><span style="display:flex;"><span>      type <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;l&#34;</span>, xname <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;x&#34;</span>, xlab <span style="color:#f92672">=</span> xname, ylab <span style="color:#f92672">=</span> <span style="color:#66d9ef">NULL</span>,
</span></span><span style="display:flex;"><span>      log <span style="color:#f92672">=</span> <span style="color:#66d9ef">NULL</span>, xlim <span style="color:#f92672">=</span> <span style="color:#66d9ef">NULL</span>, <span style="color:#66d9ef">...</span>)
</span></span><span style="display:flex;"><span><span style="color:#75715e"># expr：函数名称或一个关于变量x的函数表达式；</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># from，to：表示绘图的起止范围；</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># n：一个整数值，表示x取值的数量；</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># add：是一个逻辑值，当为TRUE时，表示将绘图添加到已存在的绘图中；</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># type：与plot函数中type含义相同</span>
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-r" data-lang="r"><span style="display:flex;"><span><span style="color:#75715e">#定义公式</span>
</span></span><span style="display:flex;"><span>sigmoid <span style="color:#f92672">&lt;-</span> <span style="color:#a6e22e">function</span>(x) <span style="color:#ae81ff">1</span><span style="color:#f92672">/</span>(<span style="color:#ae81ff">1</span><span style="color:#f92672">+</span><span style="color:#a6e22e">exp</span>(<span style="color:#f92672">-</span>x))
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">#画sigmid图像</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">curve</span>(sigmoid,<span style="color:#ae81ff">-10</span>,<span style="color:#ae81ff">10</span>)
</span></span></code></pre></div><p><img src="https://cdn.jsdelivr.net/gh/zscmmm/imgs2208save@master/img/2020081512unnamed-chunk-1-1.png" alt=""></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-r" data-lang="r"><span style="display:flex;"><span><span style="color:#75715e">#画sin(x)函数图像</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">curve</span>(sin,<span style="color:#ae81ff">-10</span>,<span style="color:#ae81ff">10</span>)
</span></span></code></pre></div><p><img src="https://cdn.jsdelivr.net/gh/zscmmm/imgs2208save@master/img/2020081512unnamed-chunk-1-2.png" alt=""></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-r" data-lang="r"><span style="display:flex;"><span><span style="color:#75715e"># 画y=x^2的图像</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">curve</span>(x^2,<span style="color:#ae81ff">-10</span>,<span style="color:#ae81ff">10</span>)
</span></span></code></pre></div><p><img src="https://cdn.jsdelivr.net/gh/zscmmm/imgs2208save@master/img/2020081512unnamed-chunk-1-3.png" alt=""></p>
<h3 id="方法二-ggplot2">方法二: ggplot2</h3>
<p>首先介绍两个映射</p>
<ul>
<li><code>geom_path()</code> 按照观测值在数据中出现的顺序连接观测值(如果画函数图,推荐此映射,原因后面知晓)。</li>
<li><code>geom_line()</code>按变量在x轴上的顺序连接它们。</li>
</ul>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-r" data-lang="r"><span style="display:flex;"><span><span style="color:#a6e22e">library</span>(ggplot2)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 定义函数</span>
</span></span><span style="display:flex;"><span>sigmoid <span style="color:#f92672">&lt;-</span> <span style="color:#a6e22e">function</span>(x) <span style="color:#ae81ff">1</span><span style="color:#f92672">/</span>(<span style="color:#ae81ff">1</span><span style="color:#f92672">+</span><span style="color:#a6e22e">exp</span>(<span style="color:#f92672">-</span>x))
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 创建数据点</span>
</span></span><span style="display:flex;"><span>x<span style="color:#f92672">&lt;-</span><span style="color:#a6e22e">seq</span>(<span style="color:#ae81ff">-5</span>, <span style="color:#ae81ff">5</span>, by<span style="color:#f92672">=</span><span style="color:#ae81ff">0.01</span>)
</span></span><span style="display:flex;"><span>y<span style="color:#f92672">&lt;-</span><span style="color:#a6e22e">sigmoid</span>(x)
</span></span><span style="display:flex;"><span>df<span style="color:#f92672">&lt;-</span><span style="color:#a6e22e">data.frame</span>(x, y)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 用ggplot2来画图</span>
</span></span><span style="display:flex;"><span>g <span style="color:#f92672">&lt;-</span> <span style="color:#a6e22e">ggplot</span>(df, <span style="color:#a6e22e">aes</span>(x,y))
</span></span><span style="display:flex;"><span>g <span style="color:#f92672">&lt;-</span> g <span style="color:#f92672">+</span> <span style="color:#a6e22e">geom_path</span>(col<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;red&#39;</span>) <span style="color:#75715e">## 用geom_line 替代也是可以的,但不推荐</span>
</span></span><span style="display:flex;"><span>g <span style="color:#f92672">&lt;-</span> g <span style="color:#f92672">+</span> <span style="color:#a6e22e">geom_hline</span>(yintercept <span style="color:#f92672">=</span> <span style="color:#ae81ff">0.5</span>) <span style="color:#f92672">+</span> <span style="color:#a6e22e">geom_vline</span>(xintercept <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>) <span style="color:#75715e">#坐标轴</span>
</span></span><span style="display:flex;"><span>g <span style="color:#f92672">&lt;-</span> g <span style="color:#f92672">+</span>  <span style="color:#a6e22e">labs</span>(title<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;sigmoid&#34;</span>, x<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;x&#34;</span>, y<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;y&#34;</span>)
</span></span><span style="display:flex;"><span>g <span style="color:#f92672">&lt;-</span> g <span style="color:#f92672">+</span><span style="color:#a6e22e">theme</span>(plot.title <span style="color:#f92672">=</span> <span style="color:#a6e22e">element_text</span>(hjust <span style="color:#f92672">=</span> <span style="color:#ae81ff">0.5</span>))  <span style="color:#75715e">#标题居中</span>
</span></span><span style="display:flex;"><span>g
</span></span></code></pre></div><p><img src="https://cdn.jsdelivr.net/gh/zscmmm/imgs2208save@master/img/2020081512unnamed-chunk-2-1.png" alt=""></p>
<h2 id="2-函数图像具有参数方程">2. 函数图像具有参数方程</h2>
<p>例如: 圆, 椭圆, 抛物线, 双曲线 方程.</p>
<h3 id="21-画x2y2--4的函数图像">2.1 画$x^2+y^2 = 4$的函数图像</h3>
<p>这里我们只以<code>ggplot2</code>为例,</p>
<p>思路一: 可以利用分段函数的思想画,先把原函数解出显示的表达式 $y = -\sqrt{4-x}$ 或者$y = \sqrt{4-x}$, 然后分段画, 这里不讲解</p>
<p>思路二: 像上面这些函数,都能表示成参数方程的形式, 利用参数方程来画该函数图像</p>
<p>首先写出圆的参数方程一般形式:</p>
<p>$$
\begin{cases}
x = rcos\theta,\
y = rsin\theta
\end{cases}
$$</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-r" data-lang="r"><span style="display:flex;"><span><span style="color:#a6e22e">library</span>(ggplot2)
</span></span><span style="display:flex;"><span>r <span style="color:#f92672">=</span> <span style="color:#ae81ff">2</span>
</span></span><span style="display:flex;"><span>theta<span style="color:#f92672">=</span><span style="color:#a6e22e">seq</span>(<span style="color:#ae81ff">0</span>, <span style="color:#ae81ff">2</span><span style="color:#f92672">*</span><span style="color:#66d9ef">pi</span>, by<span style="color:#f92672">=</span><span style="color:#ae81ff">0.001</span>)
</span></span><span style="display:flex;"><span>x <span style="color:#f92672">=</span> r<span style="color:#f92672">*</span><span style="color:#a6e22e">cos</span>(theta)
</span></span><span style="display:flex;"><span>y <span style="color:#f92672">=</span> r<span style="color:#f92672">*</span><span style="color:#a6e22e">sin</span>(theta) 
</span></span><span style="display:flex;"><span>df <span style="color:#f92672">&lt;-</span> <span style="color:#a6e22e">data.frame</span>(x, y, theta,frame <span style="color:#f92672">=</span> <span style="color:#ae81ff">1</span><span style="color:#f92672">:</span><span style="color:#a6e22e">length</span>(theta))
</span></span><span style="display:flex;"><span>g <span style="color:#f92672">&lt;-</span> <span style="color:#a6e22e">ggplot</span>(df, <span style="color:#a6e22e">aes</span>(x,y))
</span></span><span style="display:flex;"><span>g <span style="color:#f92672">&lt;-</span> g <span style="color:#f92672">+</span> <span style="color:#a6e22e">geom_path</span>(col<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;red&#39;</span>)
</span></span><span style="display:flex;"><span>g
</span></span></code></pre></div><p><img src="https://cdn.jsdelivr.net/gh/zscmmm/imgs2208save@master/img/2020081512unnamed-chunk-3-1.png" alt=""></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-r" data-lang="r"><span style="display:flex;"><span><span style="color:#75715e">###################</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">####让上面的图动起来</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">library</span>(gganimate)
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">library</span>(transformr)
</span></span><span style="display:flex;"><span>temp <span style="color:#f92672">=</span> g <span style="color:#f92672">+</span> <span style="color:#a6e22e">transition_reveal</span>(along <span style="color:#f92672">=</span> frame)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">animate</span>(temp,
</span></span><span style="display:flex;"><span>        nframes<span style="color:#f92672">=</span><span style="color:#ae81ff">100</span>,<span style="color:#75715e">#总帧数(默认)</span>
</span></span><span style="display:flex;"><span>        duration<span style="color:#f92672">=</span><span style="color:#ae81ff">10</span> <span style="color:#75715e">#总时长，单位为秒，默认为10秒</span>
</span></span><span style="display:flex;"><span>        )
</span></span></code></pre></div><p><img src="https://cdn.jsdelivr.net/gh/zscmmm/imgs2208save@master/img/2020081512unnamed-chunk-3-1.gif" alt=""></p>
<h3 id="22-椭圆">2.2 椭圆</h3>
<p>椭圆的参数方程</p>
<p>$$
\begin{cases}
x = acos\theta,\
y = bsin\theta
\end{cases}
$$</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-r" data-lang="r"><span style="display:flex;"><span><span style="color:#a6e22e">library</span>(ggplot2)
</span></span><span style="display:flex;"><span>a <span style="color:#f92672">=</span> <span style="color:#ae81ff">2</span>
</span></span><span style="display:flex;"><span>b <span style="color:#f92672">=</span> <span style="color:#ae81ff">3</span>
</span></span><span style="display:flex;"><span>theta<span style="color:#f92672">=</span><span style="color:#a6e22e">seq</span>(<span style="color:#ae81ff">0</span>, <span style="color:#ae81ff">2</span><span style="color:#f92672">*</span><span style="color:#66d9ef">pi</span>, by<span style="color:#f92672">=</span><span style="color:#ae81ff">0.001</span>)
</span></span><span style="display:flex;"><span>x <span style="color:#f92672">=</span> a<span style="color:#f92672">*</span><span style="color:#a6e22e">cos</span>(theta)
</span></span><span style="display:flex;"><span>y <span style="color:#f92672">=</span> b<span style="color:#f92672">*</span><span style="color:#a6e22e">sin</span>(theta) 
</span></span><span style="display:flex;"><span>df <span style="color:#f92672">&lt;-</span> <span style="color:#a6e22e">data.frame</span>(x, y, theta,frame <span style="color:#f92672">=</span> <span style="color:#ae81ff">1</span><span style="color:#f92672">:</span><span style="color:#a6e22e">length</span>(theta))
</span></span><span style="display:flex;"><span>g <span style="color:#f92672">&lt;-</span> <span style="color:#a6e22e">ggplot</span>(df, <span style="color:#a6e22e">aes</span>(x,y))
</span></span><span style="display:flex;"><span>g <span style="color:#f92672">&lt;-</span> g <span style="color:#f92672">+</span> <span style="color:#a6e22e">geom_path</span>(col<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;red&#39;</span>)
</span></span><span style="display:flex;"><span><span style="color:#75715e">## 让图动起来</span>
</span></span><span style="display:flex;"><span>temp <span style="color:#f92672">=</span> g <span style="color:#f92672">+</span> <span style="color:#a6e22e">transition_reveal</span>(along <span style="color:#f92672">=</span> frame)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">animate</span>(temp)
</span></span></code></pre></div><p><img src="https://cdn.jsdelivr.net/gh/zscmmm/imgs2208save@master/img/2020081512unnamed-chunk-4-1.gif" alt=""></p>
<h3 id="23-抛物线">2.3 抛物线</h3>
<p>抛物线参数方程</p>
<p>$$
\begin{cases}
x = 2pt^2, \
y = 2pt
\end{cases} (t为参数, t \in R)
$$</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-r" data-lang="r"><span style="display:flex;"><span>p <span style="color:#f92672">=</span> <span style="color:#ae81ff">4</span>
</span></span><span style="display:flex;"><span>t <span style="color:#f92672">=</span> <span style="color:#a6e22e">seq</span>(<span style="color:#ae81ff">3</span>,<span style="color:#ae81ff">-3</span>,<span style="color:#ae81ff">-0.2</span>)
</span></span><span style="display:flex;"><span>x <span style="color:#f92672">=</span> <span style="color:#ae81ff">2</span><span style="color:#f92672">*</span>p<span style="color:#f92672">*</span>t^2
</span></span><span style="display:flex;"><span>y <span style="color:#f92672">=</span> <span style="color:#ae81ff">2</span><span style="color:#f92672">*</span>p<span style="color:#f92672">*</span>t 
</span></span><span style="display:flex;"><span>df <span style="color:#f92672">&lt;-</span> <span style="color:#a6e22e">data.frame</span>(x, y, t,frame <span style="color:#f92672">=</span> <span style="color:#ae81ff">1</span><span style="color:#f92672">:</span><span style="color:#a6e22e">length</span>(t))
</span></span><span style="display:flex;"><span>g <span style="color:#f92672">&lt;-</span> <span style="color:#a6e22e">ggplot</span>(df, <span style="color:#a6e22e">aes</span>(x,y))
</span></span><span style="display:flex;"><span>g <span style="color:#f92672">&lt;-</span> g <span style="color:#f92672">+</span> <span style="color:#a6e22e">geom_path</span>(col<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;red&#39;</span>)
</span></span><span style="display:flex;"><span><span style="color:#75715e">## 让图动起来</span>
</span></span><span style="display:flex;"><span>temp <span style="color:#f92672">=</span> g <span style="color:#f92672">+</span> <span style="color:#a6e22e">transition_reveal</span>(along <span style="color:#f92672">=</span> frame)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">animate</span>(temp)
</span></span></code></pre></div><p><img src="https://cdn.jsdelivr.net/gh/zscmmm/imgs2208save@master/img/2020081512unnamed-chunk-5-1.gif" alt=""></p>
<h3 id="24-双曲线">2.4 双曲线</h3>
<p>在ggplot中没有找到好的画法</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-r" data-lang="r"><span style="display:flex;"><span>a <span style="color:#f92672">=</span> <span style="color:#ae81ff">3</span>
</span></span><span style="display:flex;"><span>b <span style="color:#f92672">=</span> <span style="color:#ae81ff">1</span>
</span></span><span style="display:flex;"><span>theta<span style="color:#f92672">=</span><span style="color:#a6e22e">round</span>(<span style="color:#a6e22e">seq</span>(<span style="color:#ae81ff">0</span>,<span style="color:#ae81ff">2</span><span style="color:#f92672">*</span><span style="color:#66d9ef">pi</span>,<span style="color:#ae81ff">0.2</span>),<span style="color:#ae81ff">2</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>x <span style="color:#f92672">=</span> a<span style="color:#f92672">/</span><span style="color:#a6e22e">cos</span>(theta)
</span></span><span style="display:flex;"><span>y <span style="color:#f92672">=</span> b<span style="color:#f92672">*</span><span style="color:#a6e22e">tan</span>(theta) 
</span></span><span style="display:flex;"><span>df <span style="color:#f92672">&lt;-</span> <span style="color:#a6e22e">data.frame</span>(x, y, theta,frame <span style="color:#f92672">=</span> <span style="color:#ae81ff">1</span><span style="color:#f92672">:</span><span style="color:#a6e22e">length</span>(theta))
</span></span><span style="display:flex;"><span>g <span style="color:#f92672">&lt;-</span> <span style="color:#a6e22e">ggplot</span>(df, <span style="color:#a6e22e">aes</span>(x,y))
</span></span><span style="display:flex;"><span>g <span style="color:#f92672">&lt;-</span> g <span style="color:#f92672">+</span> <span style="color:#a6e22e">geom_path</span>(col<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;red&#39;</span>)
</span></span><span style="display:flex;"><span>g
</span></span></code></pre></div><p><img src="https://cdn.jsdelivr.net/gh/zscmmm/imgs2208save@master/img/2020081512unnamed-chunk-6-1.png" alt=""></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-r" data-lang="r"><span style="display:flex;"><span><span style="color:#75715e"># 以分段的思想来画双曲线</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>x <span style="color:#f92672">&lt;-</span> <span style="color:#a6e22e">seq</span>(<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">3</span>,length <span style="color:#f92672">=</span> <span style="color:#ae81ff">100</span>)
</span></span><span style="display:flex;"><span>y1 <span style="color:#f92672">&lt;-</span> <span style="color:#a6e22e">sqrt</span>(x^2 <span style="color:#f92672">-</span> <span style="color:#ae81ff">1</span>);y2 <span style="color:#f92672">&lt;-</span> <span style="color:#f92672">-</span><span style="color:#a6e22e">sqrt</span>(x^2 <span style="color:#f92672">-</span> <span style="color:#ae81ff">1</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>xx1 <span style="color:#f92672">=</span> <span style="color:#a6e22e">c</span>(<span style="color:#a6e22e">rev</span>(x),x); yy1 <span style="color:#f92672">=</span> <span style="color:#a6e22e">c</span>(<span style="color:#a6e22e">rev</span>(y2),y1)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>xx2 <span style="color:#f92672">=</span> <span style="color:#a6e22e">c</span>(<span style="color:#a6e22e">rev</span>(<span style="color:#f92672">-</span>x),<span style="color:#f92672">-</span>x);yy2 <span style="color:#f92672">=</span> <span style="color:#a6e22e">c</span>(<span style="color:#a6e22e">rev</span>(y2),y1)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>df <span style="color:#f92672">&lt;-</span> <span style="color:#a6e22e">data.frame</span>(xx1,yy1,xx2,yy2, frame <span style="color:#f92672">=</span> <span style="color:#ae81ff">1</span><span style="color:#f92672">:</span><span style="color:#a6e22e">length</span>(xx1))
</span></span><span style="display:flex;"><span>g <span style="color:#f92672">&lt;-</span> <span style="color:#a6e22e">ggplot</span>(df) <span style="color:#f92672">+</span> <span style="color:#a6e22e">geom_path</span>(<span style="color:#a6e22e">aes</span>(xx1,yy1),col<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;red&#39;</span>) 
</span></span><span style="display:flex;"><span>g <span style="color:#f92672">&lt;-</span> g <span style="color:#f92672">+</span> <span style="color:#a6e22e">geom_path</span>(<span style="color:#a6e22e">aes</span>(xx2,yy2),col<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;blue&#39;</span>)
</span></span><span style="display:flex;"><span>g 
</span></span></code></pre></div><p><img src="https://cdn.jsdelivr.net/gh/zscmmm/imgs2208save@master/img/2020081512unnamed-chunk-7-1.png" alt=""></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-r" data-lang="r"><span style="display:flex;"><span>temp <span style="color:#f92672">=</span> g <span style="color:#f92672">+</span> <span style="color:#a6e22e">transition_reveal</span>(along <span style="color:#f92672">=</span> frame)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">animate</span>(temp)
</span></span></code></pre></div><p><img src="https://cdn.jsdelivr.net/gh/zscmmm/imgs2208save@master/img/2020081512unnamed-chunk-7-1.gif" alt=""></p>
<h3 id="24-lissajous-曲线利萨如曲线">2.4 Lissajous 曲线(利萨如曲线)</h3>
<p>参数方程:</p>
<p>$$
\begin{cases}
x = asin(p\theta),\
y = 2sin(q\theta+\varphi)
\end{cases}
$$</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-r" data-lang="r"><span style="display:flex;"><span>a <span style="color:#f92672">=</span> <span style="color:#ae81ff">1</span>
</span></span><span style="display:flex;"><span>b <span style="color:#f92672">=</span> <span style="color:#ae81ff">1</span>
</span></span><span style="display:flex;"><span>phi <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>
</span></span><span style="display:flex;"><span>p <span style="color:#f92672">=</span> <span style="color:#ae81ff">1</span>
</span></span><span style="display:flex;"><span>q <span style="color:#f92672">=</span> <span style="color:#ae81ff">2</span>
</span></span><span style="display:flex;"><span>t <span style="color:#f92672">=</span> <span style="color:#a6e22e">seq</span>(<span style="color:#ae81ff">0</span>, <span style="color:#ae81ff">2</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">pi</span>, by <span style="color:#f92672">=</span> <span style="color:#ae81ff">0.001</span>)
</span></span><span style="display:flex;"><span>x <span style="color:#f92672">=</span> a <span style="color:#f92672">*</span> <span style="color:#a6e22e">sin</span>(p <span style="color:#f92672">*</span> t)
</span></span><span style="display:flex;"><span>y <span style="color:#f92672">=</span> b <span style="color:#f92672">*</span> <span style="color:#a6e22e">sin</span>(q <span style="color:#f92672">*</span> t <span style="color:#f92672">+</span> phi)
</span></span><span style="display:flex;"><span>df <span style="color:#f92672">&lt;-</span> <span style="color:#a6e22e">data.frame</span>(x, y, t, frame <span style="color:#f92672">=</span> <span style="color:#ae81ff">1</span><span style="color:#f92672">:</span><span style="color:#a6e22e">length</span>(x))
</span></span><span style="display:flex;"><span>g <span style="color:#f92672">&lt;-</span> <span style="color:#a6e22e">ggplot</span>(df, <span style="color:#a6e22e">aes</span>(x, y))
</span></span><span style="display:flex;"><span>g <span style="color:#f92672">&lt;-</span> g <span style="color:#f92672">+</span> <span style="color:#a6e22e">geom_path</span>(col <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;red&#39;</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>temp <span style="color:#f92672">=</span> g <span style="color:#f92672">+</span> <span style="color:#a6e22e">transition_reveal</span>(along <span style="color:#f92672">=</span> frame)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">animate</span>(temp)
</span></span></code></pre></div><p><img src="https://cdn.jsdelivr.net/gh/zscmmm/imgs2208save@master/img/2020081512unnamed-chunk-8-1.gif" alt=""></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-r" data-lang="r"><span style="display:flex;"><span>a <span style="color:#f92672">=</span> <span style="color:#ae81ff">3</span>
</span></span><span style="display:flex;"><span>b <span style="color:#f92672">=</span> <span style="color:#ae81ff">12</span>
</span></span><span style="display:flex;"><span>phi <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>
</span></span><span style="display:flex;"><span>p <span style="color:#f92672">=</span> <span style="color:#ae81ff">13</span>
</span></span><span style="display:flex;"><span>q <span style="color:#f92672">=</span> <span style="color:#ae81ff">18</span>
</span></span><span style="display:flex;"><span>t <span style="color:#f92672">=</span> <span style="color:#a6e22e">seq</span>(<span style="color:#ae81ff">0</span>, <span style="color:#ae81ff">2</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">pi</span>, by <span style="color:#f92672">=</span> <span style="color:#ae81ff">0.001</span>)
</span></span><span style="display:flex;"><span>x <span style="color:#f92672">=</span> a <span style="color:#f92672">*</span> <span style="color:#a6e22e">sin</span>(p <span style="color:#f92672">*</span> t)
</span></span><span style="display:flex;"><span>y <span style="color:#f92672">=</span> b <span style="color:#f92672">*</span> <span style="color:#a6e22e">sin</span>(q <span style="color:#f92672">*</span> t <span style="color:#f92672">+</span> phi)
</span></span><span style="display:flex;"><span>df <span style="color:#f92672">&lt;-</span> <span style="color:#a6e22e">data.frame</span>(x, y, t, frame <span style="color:#f92672">=</span> <span style="color:#ae81ff">1</span><span style="color:#f92672">:</span><span style="color:#a6e22e">length</span>(t))
</span></span><span style="display:flex;"><span>g <span style="color:#f92672">&lt;-</span> <span style="color:#a6e22e">ggplot</span>(df, <span style="color:#a6e22e">aes</span>(x, y))
</span></span><span style="display:flex;"><span>g <span style="color:#f92672">&lt;-</span> g <span style="color:#f92672">+</span> <span style="color:#a6e22e">geom_path</span>(col <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;red&#39;</span>)
</span></span><span style="display:flex;"><span>g
</span></span></code></pre></div><p><img src="https://cdn.jsdelivr.net/gh/zscmmm/imgs2208save@master/img/2020081512unnamed-chunk-9-1.png" alt=""></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-r" data-lang="r"><span style="display:flex;"><span>temp <span style="color:#f92672">=</span> g <span style="color:#f92672">+</span> <span style="color:#a6e22e">transition_reveal</span>(along <span style="color:#f92672">=</span> frame)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">animate</span>(temp)
</span></span></code></pre></div><p><img src="https://cdn.jsdelivr.net/gh/zscmmm/imgs2208save@master/uPic/2020101108yasuohou.gif" alt=""></p>
<h2 id="3-隐函数">3 .隐函数</h2>
<p>这个暂时不知道, 且不常见,建议找MATLAB这种专业软件画或者ggb也行.</p>

        </section>
    </div>
    <br>
    
    




<span id="/md/2020-08-15-%E7%94%BB%E5%87%BD%E6%95%B0%E5%9B%BE%E5%83%8Fr%E8%AF%AD%E8%A8%80/" class="leancloud_visitors" data-flag-title="画函数图像--R语言">
  <span class="post-meta-item-text">文章总阅读量 </span>
  <span class="leancloud-visitors-count"><i class="leancloud-visitors-count"></i></span>次;
  <p></p>
</span>



    

    
    
    <button id="edit-button" class="icon-button" type="button" title="Fork and edit" aria-label="Fork and edit" aria-haspopup="true" aria-expanded="false" aria-controls="edit">
        <i class="fa fa-edit">编辑本文</i>
    </button>
    
    
    

    <br>
    <hr>
    <li style="float:left;list-style:none">
        
        <a class="previous" href="/md/2020-08-14-gb7714/"> 上一篇: latex 参考文献格式: GB/7714</a>
        
    </li>
    <li style="float:right;list-style:none">
        
        <a class="next" href="/md/2020-08-16-r%E5%8A%A8%E7%94%BB/"> 下一篇: R动画</a>
        
    </li>
     
    
    <script src="/js/copyCode.js"></script>
    <script src="/js/tooltips.js"></script>
    
   
    <script>
    [].slice.call(document.querySelectorAll('table')).forEach(function(el) {
        var wrapper = document.createElement('div');
        wrapper.className = 'table-area';
        el.parentNode.insertBefore(wrapper, el);
        el.parentNode.removeChild(el);
        wrapper.appendChild(el);
        $("table").wrap("<div class='table-area'></div>");
    })
    </script>

    
<br>
<hr>


<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-111691389-1"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag() { dataLayer.push(arguments); }
  gtag('js', new Date());

  gtag('config', 'UA-111691389-1');
</script>




      
      
      

       
      
      
      <script>
              document.getElementById("edit-button").addEventListener("click", function () {
                  var editWindow = window.open("https:\/\/github.com\/zoushucai\/blogmmm/edit/master/content/md\/2020-08-15-画函数图像R语言.md");
              });</script>
      
          




<script>
  function resizeIframe(obj) {
    obj.style.height = obj.contentWindow.document.body.scrollHeight + 'px';
  } 
</script>



    </style>
    <script type="text/javascript">
    function showdiv(){
        document.getElementById("divtocTableOfContents").style.display="block";
        document.getElementById("strHref").innerHTML="目录收起-";
        document.getElementById('divTableOfContents').style.width="22%";
        document.getElementById('divTableOfContents').style.height="55%";
        document.getElementById('divTableOfContents').style.top="25%";
        document.getElementById('divTableOfContents').style.bottom="5%";
        document.getElementById("strHref").href="javascript:hidediv()";
    }
    function hidediv(){
        document.getElementById("divtocTableOfContents").style.display="none";
        document.getElementById("strHref").innerHTML="目录展开+";
        document.getElementById("strHref").href="javascript:showdiv()";
        document.getElementById('divTableOfContents').style.width="10%";
        document.getElementById('divTableOfContents').style.height="5%";
    }
    </script>
</body>

</html>
</div> 







    <script defer src="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/contrib/mathtex-script-type.min.js" integrity="sha384-LJ2FmexL77rmGm6SIpxq7y+XA6bkLzGZEgCywzKOZG/ws4va9fUVu2neMjvc3zdv" crossorigin="anonymous"></script>

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css">
    <script defer src="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.js"></script>
    <script defer src="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/contrib/auto-render.min.js"></script>
    <script>
        document.addEventListener("DOMContentLoaded", function() {
            renderMathInElement(document.body, {
            delimiters: [
                            {left: "$$", right: "$$", display: true},
                            {left: "$", right: "$", display: false},
                            {left: "\\(", right: "\\)", display: false},
                            {left: "\\[", right: "\\]", display: true}
                        ]
            });
        });
    </script>













<br>
<div class="inner">
              
            
          
          
  
          
  
  <div id="vcomments"></div>
  
  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  
  <script src='//unpkg.com/valine/dist/Valine.min.js'></script>
  <script type="text/javascript">
    new Valine({
        el: '#vcomments' ,
        appId: 'HfHPKPkLa0cBEDPcdBAHuqMv-gzGzoHsz',
        appKey: 'r5RJAasN8e0mB9sq6y9pEcX0',
        lang:'zh-CN',
        notify:  false , 
        verify:  false  ,
        avatar:'identicon', 
        placeholder: '说点什么吧...',
        visitor:  true 
    });
  </script>

</div>

<br>
<br>
<footer>
    <p style="float:right;margin-right: 5%;margin-top: 0%;">
        &copy; 2022 <a href="https://github.com/zoushucai">zsc</a>
      </p>
</footer>
<br>
<br>


